package com.sunshine.scw.base.dao.query;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 约束查询类
 * @author xiao
 * @version 1.0
 */
public class QueryRestriction implements Serializable {

	private static final long serialVersionUID = 5241643586248027294L;

	// 操作符
	public static final int OP_EQ = 1, OP_LE = 2, OP_NOT_IN = 3, OP_GT = 4,
			OP_LT = 5, OP_GE = 6, OP_IS_NULL = 7, OP_IS_NOT_NULL = 8,
			OP_NE = 9, OP_IN = 10, OP_LIKE = 11, OP_AND = 12, OP_OR = 13,
			OP_EMPTY = 14, OP_NOT_EMPTY = 15, OP_ILIKE = 16;

	/**
	 * 属性名
	 */
	protected String property;
	/**
	 * 属性值
	 */
	protected Object value;

	// 操作符
	protected int ioperator;

	public QueryRestriction() {
	}

	public QueryRestriction(String property, Object value, int ioperator) {
		this.property = property;
		this.value = value;
		this.ioperator = ioperator;
	}

	/**
	 * 创建 "==" 约束
	 */
	public static QueryRestriction eq(String property, Object value) {
		return new QueryRestriction(property, value, OP_EQ);
	}

	/**
	 * 创建 "<=" 约束
	 */
	public static QueryRestriction le(String property, Object value) {
		return new QueryRestriction(property, value, OP_LE);
	}

	/**
	 * 创建 ">" 约束
	 */
	public static QueryRestriction gt(String property, Object value) {
		return new QueryRestriction(property, value, OP_GT);
	}

/**
	 * 创建 "<" 约束
	 */
	public static QueryRestriction lt(String property, Object value) {
		return new QueryRestriction(property, value, OP_LT);
	}

	/**
	 * 创建 ">=" 约束
	 */
	public static QueryRestriction ge(String property, Object value) {
		return new QueryRestriction(property, value, OP_GE);
	}

	/**
	 * 创建 "!=" 约束
	 */
	public static QueryRestriction ne(String property, Object value) {
		return new QueryRestriction(property, value, OP_NE);
	}

	/**
	 * 创建 "in" 约束
	 */
	public static QueryRestriction in(String property, Object value) {
		return new QueryRestriction(property, value, OP_IN);
	}

	/**
	 * 创建 "like" 约束
	 */
	public static QueryRestriction like(String property, Object value) {
		return new QueryRestriction(property, value, OP_LIKE);
	}

	/**
	 * 创建 "ilike" 约束
	 */
	public static QueryRestriction ilike(String property, Object value) {
		return new QueryRestriction(property, value, OP_ILIKE);
	}

	/**
	 * 创建 "is null" 约束
	 */
	public static QueryRestriction isNull(String property, Object value) {
		return new QueryRestriction(property, true, OP_IS_NULL);
	}

	/**
	 * 创建 "is not null" 约束
	 */
	public static QueryRestriction isNotNull(String property, Object value) {
		return new QueryRestriction(property, true, OP_IS_NOT_NULL);
	}

	/**
	 * 创建 "IN" 约束
	 */
	@SuppressWarnings("unchecked")
	public static QueryRestriction in(String property, Collection value) {
		return new QueryRestriction(property, value, OP_IN);
	}

	/**
	 * 创建 "IN" 约束
	 */
	public static QueryRestriction in(String property, Object[] value) {
		return new QueryRestriction(property, value, OP_IN);
	}

	/**
	 * 创建"NOT IN"约束
	 */
	@SuppressWarnings("unchecked")
	public static QueryRestriction notIn(String property, Collection value) {
		return new QueryRestriction(property, value, OP_NOT_IN);
	}

	/**
	 * 创建"not in"约束
	 */
	public static QueryRestriction notIn(String property, Object[] value) {
		return new QueryRestriction(property, value, OP_NOT_IN);
	}

	/**
	 * 创建"is empty"约束
	 */
	public static QueryRestriction isEmpty(String property) {
		return new QueryRestriction(property, true, OP_EMPTY);
	}

	/**
	 * 创建 "is not empty" 约束
	 */
	public static QueryRestriction isNotEmpty(String property) {
		return new QueryRestriction(property, true, OP_NOT_EMPTY);
	}

	/**
	 * 创建 "or" 约束
	 */
	public static QueryRestriction or(QueryRestriction... restrictions) {
		QueryRestriction cond = and(restrictions);
		cond.property = "OR";
		cond.ioperator = OP_OR;
		return cond;
	}

	/**
	 * 创建 "and" 约束
	 */
	public static QueryRestriction and(QueryRestriction... restrictions) {
		QueryRestriction restri = new QueryRestriction("AND", true, OP_AND);
		for (QueryRestriction restriction : restrictions) {
			restri.add(restriction);
		}
		return restri;
	}

	/**
	 * 添加约束条件
	 */
	@SuppressWarnings("unchecked")
	public void add(QueryRestriction restriction) {
		if (value == null || !(value instanceof List)) {
			value = new ArrayList();
		}
		((List) value).add(restriction);
	}

	public int getIoperator() {
		return ioperator;
	}

	public void setIoperator(int ioperator) {
		this.ioperator = ioperator;
	}

	public String getProperty() {
		return property;
	}

	public Object getValue() {
		return value;
	}

}
